package servlet;

import pojo.Tcm;
import service.TcmService;
import util.PageUtil;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 *
 * @author lzj
 *
 */
@WebServlet("/TcmServlet")
public class TcmServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    TcmService service =new TcmService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        String action = request.getParameter("action");
        if("list".equals(action)) { // 管理页显示学生列表
            int curPage=Integer.parseInt(request.getParameter("curPage"));
            request.setAttribute("curPage", curPage);
            PageUtil<Tcm> pageUtil= service.getTcmList(curPage);
            request.setAttribute("tcmList", pageUtil);
            request.getRequestDispatcher("/WEB-INF/jsp/TcmList.jsp?curPage=1").forward(request, response);
        }
        else if("search".equals(action)){
            String key=request.getParameter("key");
            int flag=Integer.parseInt(request.getParameter("flag"));
           PageUtil pageUtil= service.getQueryTcmList(key,flag);
            request.setAttribute("curPage", 1);
            request.setAttribute("tcmList", pageUtil);
            request.getRequestDispatcher("/WEB-INF/jsp/TcmList.jsp?curPage=1").forward(request, response);
        }
        else if("detail".equals(action)){
            int tcmid=Integer.parseInt(request.getParameter("tcmid"));
            request.setAttribute("detail",service.getTcmDetail(tcmid));
            request.getRequestDispatcher("/WEB-INF/jsp/TcmDetail.jsp").forward(request, response);
        }
        else if("editUI".equals(action)) {
            Tcm tcm=service.getCurTcm(Integer.parseInt(request.getParameter("id")));
            request.setAttribute("curTcm",tcm);
            request.getRequestDispatcher("/WEB-INF/jsp/TcmEdit.jsp"). forward(request, response);
        }
        else if("editProc".equals(action)) {
            Tcm tcm = new Tcm();
            tcm.setId(Integer.parseInt(request.getParameter("id")));
            service.updateTcm(autoWiredTcm(request,tcm));
            response.sendRedirect("TcmServlet?action=list&curPage=1");
        }
        else if("addUI".equals(action)) {
            request.getRequestDispatcher("/WEB-INF/jsp/TcmAdd.jsp"). forward(request, response);
        }
        else if("addProc".equals(action)) {
            Tcm tcm = new Tcm();
            service.addTcm(autoWiredTcm(request,tcm));
            response.sendRedirect("TcmServlet?action=list&curPage=1");
        }
        else if("delete".equals(action)) {
            int id=Integer.parseInt(request.getParameter("id"));
            service.deleteTcm(id);
            response.sendRedirect("TcmServlet?action=list&curPage=1");
        }

    }
    private Tcm autoWiredTcm(HttpServletRequest request,Tcm tcm){
        tcm.setNumber(Integer.parseInt(request.getParameter("number")));
        tcm.setBjyw(request.getParameter("bjyw"));
        tcm.setAlias(request.getParameter("alias"));
        tcm.setChandi(request.getParameter("chandi"));
        tcm.setDosage(request.getParameter("dosage"));
        tcm.setName(request.getParameter("name"));
        tcm.setNote(request.getParameter("note"));
        tcm.setProcessing(request.getParameter("processing"));
        tcm.setTaboo(request.getParameter("taboo"));
        tcm.setType(Integer.parseInt(request.getParameter("type")));
        tcm.setXingwei(request.getParameter("xingwei"));
        tcm.setZhuzhi(request.getParameter("zhuzhi"));
        return tcm;
    }
//
//    private void detail(HttpServletRequest request) throws IOException {
//        int id = Integer.parseInt(request.getParameter("id"));
//        StudentService service =new StudentService();
//        request.setAttribute("studentDetail",service.getStudent(id));
//
//    }
//
//    private void delete(HttpServletRequest request) throws IOException {
//        int id = Integer.parseInt(request.getParameter("id"));
//        StudentService service =new StudentService();
//        service.deleteStudent(id);
//    }
}
